package com.service.scheduler.sched;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;

import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.scheduler.job.CacheRefreshJob;
import com.service.scheduler.job.GarbageCommntRefreshJob;
import com.service.scheduler.job.PointTimesRefreshJob;
import com.service.scheduler.server.SchedulerCreator;

/**
 * 任务刷新
 * @author lj
 *
 */
public class CacheSched {
	
	private Scheduler sched;
	
	public void init(){
		try {
			sched = SchedulerCreator.createScheduler(CacheSched.class.getSimpleName(),1,5);
			initCacheRefresh();
			initPointTimesRefresh();
			initGarbageCommntRefresh();
			CacheManager.instance().schedMap.put(sched.getSchedulerName(),sched);
		} catch (SchedulerException e) {
			Logger.err(e);
		}
	}
	
	/**
	 * 初始化缓存刷新任务(每小时执行)
	 * @throws SchedulerException
	 */
	private void initCacheRefresh() throws SchedulerException{
		JobKey jobKey = SchedulerCreator.createJobKey("refresh", "com.service.cache");
		TriggerKey triggerKey = SchedulerCreator.createTriggerKey("refresh","com.service.cache");
		String cronExpr = SchedulerCreator.createHourExpr(1);
		JobDetail job = SchedulerCreator.createJob(jobKey, CacheRefreshJob.class);
		CronTrigger cronTrigger = SchedulerCreator.createCronTrigger(triggerKey,cronExpr);
		sched.scheduleJob(job, cronTrigger);
	}
	
	/**
	 * 初始化补点次数(每天0:10分执行)
	 * @throws SchedulerException
	 */
	private void initPointTimesRefresh() throws SchedulerException{
		JobKey jobKey = SchedulerCreator.createJobKey("pointTimes", "com.service.cache");
		TriggerKey triggerKey = SchedulerCreator.createTriggerKey("pointTimes","com.service.cache");
		String cronExpr = "00 10 00 * * ?";
		JobDetail job = SchedulerCreator.createJob(jobKey, PointTimesRefreshJob.class);
		CronTrigger cronTrigger = SchedulerCreator.createCronTrigger(triggerKey,cronExpr);
		sched.scheduleJob(job, cronTrigger);
	}
	
	/**
	 * 刷新垃圾称重 小区信息
	 * @throws SchedulerException
	 */
	private void initGarbageCommntRefresh() throws SchedulerException {
		JobKey jobKey = SchedulerCreator.createJobKey("garbageCommnt", "com.service.cache");
		TriggerKey triggerKey = SchedulerCreator.createTriggerKey("garbageCommnt", "com.service.cache");
		String cronExpr = "00 20 00 * * ?";
		JobDetail job = SchedulerCreator.createJob(jobKey, GarbageCommntRefreshJob.class);
		CronTrigger cronTrigger = SchedulerCreator.createCronTrigger(triggerKey, cronExpr);
		sched.scheduleJob(job, cronTrigger);
	}
	
}
